package org.sharing.design.factory;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import org.sharing.design.factory.shop.card.CardService;
import org.sharing.design.factory.shop.coupon.CouponResult;
import org.sharing.design.factory.shop.coupon.CouponService;
import org.sharing.design.factory.shop.goods.DeliverReq;
import org.sharing.design.factory.shop.goods.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: Dragon
 * @since: 2024/4/6 12:06
 */
public class PrizeController {

    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);

    public AwardResult awardToUser(AwardReq req) {
        AwardResult awardResult = new AwardResult();
        String jsonString = JSON.toJSONString(req);

        logger.info("模拟发放奖品开始");
        //优惠券
        if(req.getAwardType() == 1) {
            CouponService couponService = new CouponService();
            CouponResult couponResult = couponService.sendCoupon();
            if ("0000".equals(couponResult.getCode())) {
                awardResult = new AwardResult("0000", "发放成功");
            } else {
                awardResult = new AwardResult("0001", couponResult.getInfo());
            }
        } else if (req.getAwardType() == 2) { //实物商品
            GoodsService goodsService = new GoodsService();
            DeliverReq deliverReq = new DeliverReq();
            deliverReq.setUserName(req.getUId());
            deliverReq.setUserPhone(req.getPhoneNumber());
            deliverReq.setSku(req.getAwardNumber());
            deliverReq.setOrderId(req.getBizId());
            deliverReq.setConsigneeUserName(req.getExtMap().get("consigneeUserName"));
            deliverReq.setConsigneeUserPhone(req.getExtMap().get("consigneeUserPhone"));
            deliverReq.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));
            Boolean isSuccess = goodsService.deliverGoods(deliverReq);

            if (isSuccess) {
                awardResult = new AwardResult("0000", "发放成功");
            } else {
                awardResult = new AwardResult("0001", "发放失败");
            }
        } else if(req.getAwardType() == 3) {
            CardService cardService = new CardService();
            cardService.grantToken(req.getUId(), req.getAwardNumber());

            awardResult = new AwardResult("0000", "发放成功");
        }

        logger.info("奖品发放完成");

        return awardResult;
    }
}
